<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item :label="$t('order.orderTime') /*订单时间*/">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-date-picker
                  v-model="queryForm.date1"
                  type="daterange"
                  value-format="timestamp"
                  :picker-options="pickerOptions"
                  :range-separator="$t('order.to') /*至*/"
                  :start-placeholder="$t('order.beginDate') /*开始日期*/"
                  :end-placeholder="$t('order.endDate') /*结束日期*/"
                  align="right"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-tag
              type="info"
              :effect="time_type == 1 ? 'dark' : ''"
              @click="changeOrderTime(1)"
            >
              {{ $t('order.today') /*今日*/ }}
            </el-tag>
            <el-tag
              type="info"
              :effect="time_type == 2 ? 'dark' : ''"
              @click="changeOrderTime(2)"
            >
              {{ $t('order.yesterday') /*昨天*/ }}
            </el-tag>
            <el-tag
              type="info"
              :effect="time_type == 3 ? 'dark' : ''"
              @click="changeOrderTime(3)"
            >
              {{ $t('order.thisMonth') /*本月*/ }}
            </el-tag>
            <el-tag
              type="info"
              :effect="time_type == 4 ? 'dark' : ''"
              @click="changeOrderTime(4)"
            >
              {{ $t('order.thisYear') /*本年*/ }}
            </el-tag>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ $t('common.search') /*查询*/ }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>

      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <!-- <el-form-item label="租户账号">
            <el-input
              v-model.trim="queryForm.account"
              placeholder="请输入租户账号"
              clearable
              disabled
            />
          </el-form-item> -->
          <el-form-item :label="$t('order.orderNumber') /*订单号*/">
            <el-input
              v-model.trim="queryForm.order_no"
              :placeholder="$t('order.pleaseOrderNumber') /*请输入订单号*/"
              clearable
            />
          </el-form-item>
          <el-form-item :label="$t('order.orderType') /*订单类型*/">
            <el-select
              v-model="queryForm.order_type"
              :placeholder="$t('order.pleaseSelOrderType') /*请选择订单类型*/"
            >
              <el-option label="购买" value="1"></el-option>
              <el-option label="续费" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('order.accountType') /*账号类型*/">
            <el-select
              v-model="queryForm.account_type"
              :placeholder="$t('order.pleaseAccountType') /*请选择账号类型*/"
            >
              <el-option label="租户" value="1"></el-option>
              <el-option label="设备" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('order.chargingMode') /*收费模式*/">
            <el-select
              v-model="queryForm.buy_type"
              :placeholder="$t('order.pleaseChargingMode') /*请选择收费模式*/"
            >
              <el-option label="按个" value="1"></el-option>
              <el-option label="按套餐" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            :label="$t('accountManage.agentAccountNumber') /*代理商账号*/"
          >
            <el-input
              v-model.trim="queryForm.agent_name"
              :placeholder="
                $t(
                  'accountManage.pleaseEditAgentAccountNumber'
                ) /*请输入代理商账号*/
              "
              clearable
            />
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <div class="card-list">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-card class="box-card card-item">
            <div class="card-item-detail">
              <span class="title item-text">
                {{ $t('accountManage.tenantAccountNumber') /*租户账号*/ }}：
              </span>
              <span class="value item-text">
                {{ orderCountData.count_tanent || 0 }}
              </span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="box-card card-item">
            <div class="card-item-detail">
              <span class="title item-text">
                {{ $t('accountManage.tenantAccountNumber') /*租户账号*/ }}：
              </span>
              <span class="value item-text">
                {{ orderCountData.count_dev || 0 }}
              </span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="box-card card-item">
            <div class="card-item-detail">
              <span class="title item-text">
                {{ $t('accountManage.tenantAccountNumber') /*租户账号*/ }}：
              </span>
              <span class="value item-text">
                {{ orderCountData.meal_20 || 0 }}
              </span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="box-card card-item">
            <div class="card-item-detail">
              <span class="title item-text">
                {{ $t('accountManage.tenantAccountNumber') /*租户账号*/ }}：
              </span>
              <span class="value item-text">
                {{ orderCountData.meal_50 || 0 }}
              </span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="box-card card-item">
            <div class="card-item-detail">
              <span class="title item-text">
                {{ $t('accountManage.tenantAccountNumber') /*租户账号*/ }}：
              </span>
              <span class="value item-text">
                {{ orderCountData.meal_100 || 0 }}
              </span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card class="box-card card-item">
            <div class="card-item-detail">
              <span class="title item-text">
                {{ $t('accountManage.tenantAccountNumber') /*租户账号*/ }}：
              </span>
              <span class="value item-text">
                {{ orderCountData.meal_200 || 0 }}
              </span>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column show-overflow-tooltip type="selection"></el-table-column> -->
      <el-table-column
        show-overflow-tooltip
        prop="create_time"
        :label="$t('order.dateCreated') /*创建日期*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="order_no"
        :label="$t('order.orderNumber') /*订单号*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="order_type"
        :label="$t('order.orderType') /*订单类型*/"
      >
        <template #default="{ row }">
          <el-tag>
            {{ row.order_type == 1 ? '购买' : '续费' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="account_type"
        :label="$t('order.accountType') /*账号类型*/"
      >
        <template #default="{ row }">
          <el-tag>
            {{ row.account_type == 1 ? '租户' : '设备' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="order_time"
        :label="$t('order.buyIngredients') /*购买时长（月）*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="num_acount"
        :label="$t('accountManage.buyNumber') /*购买数量*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="buy_type"
        :label="$t('order.chargingMode') /*收费模式*/"
      >
        <template #default="{ row }">
          <el-tag>
            {{ row.buy_type == 1 ? '按个' : '按套餐' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="order_mark"
        :label="$t('accountManage.orderRemark') /*订单备注*/"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        :label="$t('common.operate') /*操作*/"
        width="300"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('order.viewDetail') /*查看详情*/ }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
  import { tjOrderMain } from '@/api/agents'
  import { orderlist } from '@/api/business.js'
  import { doDelete } from '@/api/userManagement'
  import Edit from './components/UserManagementEdit'
  export default {
    name: 'UserManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        time_type: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          username: '',
          date1: [],
          date2: '',
        },
        orderCountData: {}, //统计数据
        pickerOptions: {
          shortcuts: [
            {
              text: '最近一周',
              onClick(picker) {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                picker.$emit('pick', [start, end])
              },
            },
            {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                picker.$emit('pick', [start, end])
              },
            },
            {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date()
                const start = new Date()
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
                picker.$emit('pick', [start, end])
              },
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      changeOrderTime(oTimeType) {
        if (oTimeType == this.time_type) {
          this.time_type = -1
        } else {
          this.time_type = oTimeType
        }
        this.queryForm.time_type = this.time_type
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.uorder_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        if (this.queryForm.date1.length > 0) {
          this.queryForm.first_time = this.queryForm.date1[0]
          this.queryForm.end_time = this.queryForm.date1[1]
        }
        const { data } = await orderlist(this.queryForm)

        this.list = data.data
        this.total = data.total

        let tjData = await tjOrderMain(this.queryForm)

        this.orderCountData = tjData.data
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
<style lang="scss" scoped>
  .box-card {
    .card-item-detail {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .item-text {
        padding: 6px 0;
      }
    }
  }
</style>
